import Vue from 'vue'
import VueRouter from 'vue-router'
import Home from '../views/Home.vue'

Vue.use(VueRouter)

const routes = [
  {
    path: '/',
    name: 'Home',
    component: Home
  },
  {
    path: '/login',
    name: 'Login',
    meta:{
      title: 'Login'
    },
    component: () => import(/* webpackChunkName: "about" */ '../views/Login.vue')
  },
  {
    path: '/about',
    name: 'About',
    meta:{
      title: 'about',
      auth: true,
    },
    component: () => import(/* webpackChunkName: "about" */ '../views/About.vue')
  },
  {
    path: '/notfind',
    name: 'Notfind',
    component: () => import(/* webpackChunkName: "about" */ '../views/Notfind.vue')
  },
  {
    path: '*',
    redirect: '/notfind'
  }
]

const router = new VueRouter({
  mode: 'history',
  base: process.env.BASE_URL,
  routes
})
router.beforeEach((to, from , next) =>{
  if(to.meta.auth){ //需要校验的页面
    if(localStorage.getItem('token')){
      next()
    }else{
      next({path:'/login'})
    }
  }else{
    next();
  }
  
})

export default router
